import { Component, ElementRef } from '@angular/core';
import { IonicPage, NavController, ToastController, NavParams } from 'ionic-angular';
import { RuleRecordApi } from '../../api/rule-record/RuleRecordApi';
import { BarcodeScanner } from '@ionic-native/barcode-scanner';

@IonicPage()
@Component({
  selector: 'page-rule-scan',
  templateUrl: 'rule-scan.html'
})
export class RuleScan {

  private qrcode: string;
  private materiel: object;
  private positionId: string;
  private param: any;
  private enableRule: boolean = true;
  private hideTag: boolean = true;
  private LeaveTag: boolean = true;


  //  离开页面守卫
  ionViewCanLeave(): boolean {
    return this.LeaveTag;
  }

  constructor(private barcodeScanner: BarcodeScanner, private params: NavParams, private ele: ElementRef, public navCtrl: NavController, private toastCtrl: ToastController, private ruleRecordApi: RuleRecordApi) {
    this.materiel = {
      materielName: "",
      materielCode: "",
      materielLength: "",
      materielWidth: "",
      materielHeight: "",
      materielEmptyWeight: "",
      materielGrossWeight: "",
      companyName: "",
      projectName: "",
      planCode: "",
      planDesc: "",
      packingListCode: ""
    };
    this.positionId = params.get("positionId");
  }



  //获取焦点
  scan() {
    this.ele.nativeElement.querySelector("#scanmateriel").focus();
  }

  //输入框值改变事件
  infoChange(val) {
    this.param = {
      "qrcode": val,
    };
    this.scanMateriel(this.param);
  }

  toScan() {
    this.barcodeScanner.scan({ showTorchButton: true, resultDisplayDuration: 0 }).then((datainfo) => {
      if (datainfo.text.length != 0) {
        this.qrcode = datainfo.text;
        this.param = {
          "qrcode": datainfo.text
        };
        this.scanMateriel(this.param);
      } else {
        this.fixshowMsg("请扫描正确的二维码!");
      }
    }, (err) => {
      this.showMsg("扫描出现异常" + err);
    });
  }

  scanMateriel(param: any) {
    //请求后台接口
    const http$ = this.ruleRecordApi.scanMateriel(param);
    http$.subscribe((val: any) => {
      //this.keyboard.close();
      let scanResult = JSON.parse(val);
      if (scanResult.state) {
        // this.ele.nativeElement.querySelector("#scanmaterielbtn").click();
        this.materiel = scanResult.data;
        //  this.ele.nativeElement.querySelector(".hides").removeAttribute("hidden");
        this.enableRule = false;
        this.hideTag = false;
      } else {
        // this.keyboard.close();
        this.showMsg(scanResult.msg);
        //this.ele.nativeElement.querySelector("#scanmaterielbtn").click();
        // this.ele.nativeElement.querySelector("#scanmateriel").focus();
      }
    });
  }


  //点击打尺
  ruleAndPhoto() {
    //跳到打尺页面
    this.navCtrl.push("RuleRecord", {
      "qrcode": this.qrcode,
      "positionId": this.positionId,
      "materiel": this.materiel
    });
  }

  //点击取消返回
  goBack() {
    //this.ele.nativeElement.querySelector("[ng-reflect-klass='back-button']").click();
    this.navCtrl.pop();
  }

  showMsg(msg: string) {
    var toast = this.toastCtrl.create({
      message: msg,
      duration: 3000,
      position: 'bottom'
    });
    toast.present();
  }

  fixshowMsg(msg: string) {
    this.LeaveTag = false;
    var toast = this.toastCtrl.create({
      message: msg,
      duration: 3000,
      position: 'bottom'
    });
    toast.present();
    setTimeout(() => this.LeaveTag = true, 1000);
  }
}
